﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using HYElectricKernel;

namespace YArchitech.Plumbing
{
	public class RectLayoutNum
	{
		public RectLayoutNum(RectLayParamers rectEx)
		{
			this.rectParas = rectEx;
		}

		public List<List<XYZ>> CalcInsertPoints()
		{
			List<List<XYZ>> insertPoints = this.GetInsertPoints();
			if (this.IsEmptyPointSet(insertPoints))
			{
				return null;
			}
			return insertPoints;
		}

		private bool IsEmptyPointSet(List<List<XYZ>> pointSet)
		{
			if (pointSet == null || pointSet.Count <= 0)
			{
				return true;
			}
			foreach (List<XYZ> list in pointSet)
			{
				if (list != null && list.Count > 0)
				{
					return false;
				}
			}
			return true;
		}

		private List<List<XYZ>> GetInsertPoints()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			if (this.rectParas.RowCount <= 0 || this.rectParas.ColCount <= 0)
			{
				return null;
			}
			if (this.rectParas.RowCount == 1 && this.rectParas.ColCount == 1)
			{
				List<XYZ> list2 = new List<XYZ>();
				XYZ item = this.rectParas.PointBase + 0.5 * (this.rectParas.PointDiagonal - this.rectParas.PointBase);
				list2.Add(item);
				list.Add(list2);
				return list;
			}
			double num = Func.AngleToRadian(this.rectParas.AngleRow);
			XYZ xyz = Geometry.RotateTo(this.rectParas.PointDiagonal, this.rectParas.PointBase, XYZ.BasisZ, -num);
			this.rectParas.XDiagonalPoint = new XYZ(xyz.X, this.rectParas.PointBase.Y, xyz.Z);
			this.rectParas.YDiagonalPoint = new XYZ(this.rectParas.PointBase.X, xyz.Y, xyz.Z);
			this.rectParas.XLength = this.rectParas.PointBase.DistanceTo(this.rectParas.XDiagonalPoint);
			this.rectParas.YLength = this.rectParas.PointBase.DistanceTo(this.rectParas.YDiagonalPoint);
			this.rectParas.XSpace = ((Math.Abs(2.0 * this.rectParas.Margin + (double)this.rectParas.ColCount - 1.0) < 1E-06) ? 0.0 : (this.rectParas.XLength / (2.0 * this.rectParas.Margin + (double)this.rectParas.ColCount - 1.0)));
			this.rectParas.YSpace = ((Math.Abs(2.0 * this.rectParas.Margin + (double)this.rectParas.RowCount - 1.0) < 1E-06) ? 0.0 : (this.rectParas.YLength / (2.0 * this.rectParas.Margin + (double)this.rectParas.RowCount - 1.0)));
			if (this.rectParas.DislocationWay == 2)
			{
				list = this.GetInsertPointsOfCol();
			}
			else if (this.rectParas.DislocationWay == 1)
			{
				list = this.GetInsertPointsOfRow();
			}
			else
			{
				list = this.GetInsertPointsOfNone();
			}
			foreach (List<XYZ> list3 in list)
			{
				for (int i = 0; i < list3.Count; i++)
				{
					XYZ pt = list3[i];
					list3[i] = Geometry.RotateTo(pt, this.rectParas.PointBase, XYZ.BasisZ, num);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfNone()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			if (this.rectParas.LinkLineWay == 2)
			{
				for (int i = 0; i < this.rectParas.ColCount; i++)
				{
					List<XYZ> list2 = new List<XYZ>();
					XYZ xyz = this.rectParas.PointBase + this.rectParas.XSpace * (this.rectParas.Margin + (double)i) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int j = 0; j < this.rectParas.RowCount; j++)
					{
						XYZ item = xyz + this.rectParas.YSpace * (this.rectParas.Margin + (double)j) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						list2.Add(item);
					}
					list.Add(list2);
				}
			}
			else
			{
				for (int k = 0; k < this.rectParas.RowCount; k++)
				{
					List<XYZ> list3 = new List<XYZ>();
					XYZ xyz2 = this.rectParas.PointBase + this.rectParas.YSpace * (this.rectParas.Margin + (double)k) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int l = 0; l < this.rectParas.ColCount; l++)
					{
						XYZ item2 = xyz2 + this.rectParas.XSpace * (this.rectParas.Margin + (double)l) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						list3.Add(item2);
					}
					list.Add(list3);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfRow()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			if (this.rectParas.LinkLineWay == 2)
			{
				for (int i = 0; i < this.rectParas.ColCount; i++)
				{
					List<XYZ> list2 = new List<XYZ>();
					XYZ xyz = this.rectParas.PointBase + this.rectParas.XSpace * (this.rectParas.Margin + (double)i) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int j = 0; j < this.rectParas.RowCount; j += 2)
					{
						XYZ item = xyz + this.rectParas.YSpace * (this.rectParas.Margin + (double)j) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						list2.Add(item);
					}
					list.Add(list2);
				}
				for (int k = 0; k < this.rectParas.ColCount - 1; k++)
				{
					List<XYZ> list3 = new List<XYZ>();
					XYZ xyz2 = this.rectParas.PointBase + this.rectParas.XSpace * (this.rectParas.Margin + 0.5 + (double)k) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int l = 1; l < this.rectParas.RowCount; l += 2)
					{
						XYZ item2 = xyz2 + this.rectParas.YSpace * (this.rectParas.Margin + (double)l) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						list3.Add(item2);
					}
					list.Add(list3);
				}
			}
			else
			{
				for (int m = 0; m < this.rectParas.RowCount; m += 2)
				{
					List<XYZ> list4 = new List<XYZ>();
					XYZ xyz3 = this.rectParas.PointBase + this.rectParas.YSpace * (this.rectParas.Margin + (double)m) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int n = 0; n < this.rectParas.ColCount; n++)
					{
						XYZ item3 = xyz3 + this.rectParas.XSpace * (this.rectParas.Margin + (double)n) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						list4.Add(item3);
					}
					list.Add(list4);
				}
				for (int num = 1; num < this.rectParas.RowCount; num += 2)
				{
					List<XYZ> list5 = new List<XYZ>();
					XYZ xyz4 = this.rectParas.PointBase + this.rectParas.YSpace * (this.rectParas.Margin + (double)num) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int num2 = 0; num2 < this.rectParas.ColCount - 1; num2++)
					{
						XYZ item4 = xyz4 + this.rectParas.XSpace * (this.rectParas.Margin + 0.5 + (double)num2) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						list5.Add(item4);
					}
					list.Add(list5);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfCol()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			if (this.rectParas.LinkLineWay == 2)
			{
				for (int i = 0; i < this.rectParas.ColCount; i += 2)
				{
					List<XYZ> list2 = new List<XYZ>();
					XYZ xyz = this.rectParas.PointBase + this.rectParas.XSpace * (this.rectParas.Margin + (double)i) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int j = 0; j < this.rectParas.RowCount; j++)
					{
						XYZ item = xyz + this.rectParas.YSpace * (this.rectParas.Margin + (double)j) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						list2.Add(item);
					}
					list.Add(list2);
				}
				for (int k = 1; k < this.rectParas.ColCount; k += 2)
				{
					List<XYZ> list3 = new List<XYZ>();
					XYZ xyz2 = this.rectParas.PointBase + this.rectParas.XSpace * (this.rectParas.Margin + (double)k) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int l = 0; l < this.rectParas.RowCount - 1; l++)
					{
						XYZ item2 = xyz2 + this.rectParas.YSpace * (this.rectParas.Margin + 0.5 + (double)l) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
						list3.Add(item2);
					}
					list.Add(list3);
				}
			}
			else
			{
				for (int m = 0; m < this.rectParas.RowCount; m++)
				{
					List<XYZ> list4 = new List<XYZ>();
					XYZ xyz3 = this.rectParas.PointBase + this.rectParas.YSpace * (this.rectParas.Margin + (double)m) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int n = 0; n < this.rectParas.ColCount; n += 2)
					{
						XYZ item3 = xyz3 + this.rectParas.XSpace * (this.rectParas.Margin + (double)n) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						list4.Add(item3);
					}
					list.Add(list4);
				}
				for (int num = 0; num < this.rectParas.RowCount - 1; num++)
				{
					List<XYZ> list5 = new List<XYZ>();
					XYZ xyz4 = this.rectParas.PointBase + this.rectParas.YSpace * (this.rectParas.Margin + 0.5 + (double)num) * (this.rectParas.YDiagonalPoint - this.rectParas.PointBase).Normalize();
					for (int num2 = 1; num2 < this.rectParas.ColCount; num2 += 2)
					{
						XYZ item4 = xyz4 + this.rectParas.XSpace * (this.rectParas.Margin + (double)num2) * (this.rectParas.XDiagonalPoint - this.rectParas.PointBase).Normalize();
						list5.Add(item4);
					}
					list.Add(list5);
				}
			}
			return list;
		}

		private RectLayParamers rectParas;
	}
}
